package com.pignest.hotel.tool;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

import javax.servlet.ServletContext;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;

public class JasperCompiler {
	public static final String BASIC_PATH = "report/jasper/";
	
	
	public static Collection getSourceFiles(){
		Collection files = new HashSet();
		files.add("repair.jrxml");
		files.add("scout.jrxml");
		files.add("system_running.jrxml");
		files.add("workstat.jrxml");
		files.add("system_basic.jrxml");
		return files;
	}
	
	public static void compile(ServletContext sc){
		for (Iterator it = getSourceFiles().iterator(); it.hasNext();) {
			String file = (String) it.next();
			compile(sc.getRealPath(BASIC_PATH + file));
		}
	}
	
	private static void compile(String filename){
		String jrxml = filename.toLowerCase();
		String jasper = null;
		if(!filename.endsWith(".jrxml")){
			jrxml += ".jrxml";
		}
		jasper = jrxml.substring(0, jrxml.length() - ".jrxml".length()) + ".jasper";
		try{
			JasperCompileManager.compileReportToFile(jrxml, jasper);
		}catch(JRException e){
			System.out.println(filename);
			e.printStackTrace();
		}
	}
}
